package com.elock.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.elock.base.core.Filter;
import com.elock.base.core.Page;
import com.elock.base.core.Pageable;
import com.elock.base.core.controller.BaseController;
import com.elock.base.core.controller.ResultMsg;
import com.elock.base.core.util.ConvertUtil;
import com.elock.base.core.util.JsonUtils;
import com.elock.entity.Elock;
import com.elock.entity.SystemDict;
import com.elock.service.ElockSaleOrgService;
import com.elock.service.ElockService;
import com.elock.service.SaleOrgBaseService;
import com.elock.service.SystemDictBaseService;

/**
 * Controller - 锁
 * @author 仁德
 */
@Controller("elockController")
@RequestMapping("/elock")
public class ElockController extends BaseController {

	@Resource(name = "elockServiceImpl")
	private ElockService elockService;
	@Resource(name = "systemDictBaseServiceImpl")
	private SystemDictBaseService systemDictBaseService;
	@Resource(name = "saleOrgBaseServiceImpl")
	private SaleOrgBaseService saleOrgBaseService;
	@Resource(name = "elockSaleOrgServiceImpl")
	private ElockSaleOrgService elockSaleOrgService;

	/**
	 * 列表
	 */
	@RequestMapping(value = "/list_tb", method = RequestMethod.GET)
	public String list_tb(ModelMap model) {

		return "/elock/list_tb";
	}

	/**
	 * 列表
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Pageable pageable, ModelMap model) {

		return "/elock/list";
	}

	/**
	 * 列表数据
	 */
	@RequestMapping(value = "/list_data", method = RequestMethod.POST)
	public @ResponseBody ResultMsg list_data(String elockNum, String elockName,
			Long saleOrgId, Pageable pageable, ModelMap model) {

		Page<Map<String, Object>> page = elockService.findPage(elockNum,
				elockName,
				saleOrgId,
				null,
				pageable);
		String jsonPage = JsonUtils.toJson(page);

		return success(jsonPage);
	}

	/**
	 * 添加
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(ModelMap model) {

		List<Filter> filters = new ArrayList<Filter>();
		filters.add(Filter.eq("code", "ElockType"));
		filters.add(Filter.isNotNull("parent"));
		List<SystemDict> elockTypes = systemDictBaseService.findList(null, filters, null);
		model.addAttribute("elockTypes", elockTypes);

		filters.clear();
		filters.add(Filter.eq("code", "ElockModel"));
		filters.add(Filter.isNotNull("parent"));
		List<SystemDict> elockModels = systemDictBaseService.findList(null,
				filters,
				null);
		model.addAttribute("elockModels", elockModels);
//		model.addAttribute("saleOrg", saleOrgBaseService.getCurrentSaleOrg());

		return "/elock/add";
	}

	/**
	 * 保存
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public @ResponseBody ResultMsg save(Elock elock, Long typeId, Long saleOrgId,
			Long modelId) {

		if (ConvertUtil.isEmpty(elock.getElockNum())) {
			return error("请填写锁编号");
		}
//		if (elockService.exists(Filter.eq("elockNum", elock.getElockNum()))) {
//			return error("该锁编号已存在,请重新维护");
//		}
		Elock e = elockService.findByElockNum(elock.getElockName());
		if (e != null) {
			return error("该锁编号在系统中已存在");
		}
		if (typeId != null) {
			elock.setType(systemDictBaseService.find(typeId));
		}
		if (modelId != null) {
			elock.setModel(systemDictBaseService.find(modelId));
		}
		elockService.saveUpdate(elock, saleOrgId);

		return success().addObjX(elock.getId());
	}

	/**
	 * 编辑
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String edit(Long id, ModelMap model) {

		Elock elock = elockService.find(id);
		model.addAttribute("elock", elock);

		List<Filter> filters = new ArrayList<Filter>();
		filters.add(Filter.eq("code", "ElockType"));
		filters.add(Filter.isNotNull("parent"));
		List<SystemDict> elockTypes = systemDictBaseService.findList(null, filters, null);
		model.addAttribute("elockTypes", elockTypes);

		filters.clear();
		filters.add(Filter.eq("code", "ElockModel"));
		filters.add(Filter.isNotNull("parent"));
		List<SystemDict> elockModels = systemDictBaseService.findList(null,
				filters,
				null);
		model.addAttribute("elockModels", elockModels);

		filters.clear();
		filters.add(Filter.eq("elock", elock));
//		ElockSaleOrg elockSaleOrg = elockSaleOrgService.find(filters);
//		if (elockSaleOrg != null) {
//			model.addAttribute("saleOrg", elockSaleOrg.getSaleOrg());
//		}
//		else {
//			model.addAttribute("saleOrg", saleOrgBaseService.getCurrentSaleOrg());
//		}

		return "/elock/edit";
	}

	/**
	 * 更新
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody ResultMsg update(Elock elock, Long typeId, Long saleOrgId,
			Long modelId) {

		if (ConvertUtil.isEmpty(elock.getElockNum())) {
			return error("请填写锁编号");
		}
//		if (elockService.exists(Filter.eq("elockNum", elock.getElockNum()),
//				Filter.ne("id", elock.getId()))) {
//			return error("该锁编号已存在,请重新维护");
//		}
		Elock e = elockService.findByElockNum(elock.getElockName());
		if (e != null && e.getId() != elock.getId()) {
			return error("该锁编号在系统中已存在");
		}

		if (typeId != null) {
			elock.setType(systemDictBaseService.find(typeId));
		}
		if (modelId != null) {
			elock.setModel(systemDictBaseService.find(modelId));
		}

		elockService.saveUpdate(elock, saleOrgId);

		return success();
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody ResultMsg delete(Long[] ids) {

		elockService.delete(ids);
		return success();

	}

	/**
	 * 列表
	 */
	@RequestMapping(value = "/select_list", method = RequestMethod.GET)
	public String select_list(Pageable pageable, Integer multi, Long saleOrgId,
			ModelMap model) {

		model.addAttribute("multi", multi);
		model.addAttribute("saleOrgId", saleOrgId);
		return "/elock/select_list";
	}

	/**
	 * 列表数据
	 */
	@RequestMapping(value = "/select_list_data", method = RequestMethod.POST)
	public @ResponseBody ResultMsg select_list_data(String elockNum, String elockName,
			Long saleOrgId, Pageable pageable, ModelMap model) {

		Page<Map<String, Object>> page = elockService.findPage(elockNum,
				elockName,
				saleOrgId,
				true,
				pageable);
		String jsonPage = JsonUtils.toJson(page);

		return success(jsonPage);
	}
}
